package com.dinian.zeus.service.impl;

import com.dinian.zeus.domain.Newspaper;
import com.dinian.zeus.domain.ResponseVO;
import com.dinian.zeus.mapper.NewspaperMapper;
import com.dinian.zeus.service.NewspaperService;
import com.dinian.zeus.util.Contant;
import com.dinian.zeus.util.DateUtil;
import com.dinian.zeus.util.ExcelUtil;
import com.dinian.zeus.util.UploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author dinian
 * @Date 2018-12-20
 */
@Slf4j
@Service
public class NewspaperServiceImpl implements NewspaperService {

    @Autowired
    private NewspaperMapper newspaperMapper;

    /**
     * 查询所有报刊信息
     * @return
     */
    @Override
    public List<Newspaper> findAllNewspaper() {
        List<Newspaper> newspapers = newspaperMapper.selectNewspapers();
        log.info("init newspaper totalNum: {}",newspapers.size());
        //exception
        return newspapers;
    }

    /**
     * 根据查询条件搜索报刊信息
     * @param newspaper
     * @return
     */
    @Override
    public List<ResponseVO> searchNewspaper(Newspaper newspaper) {
        if(newspaper == null){
            return null;
        }
        List<ResponseVO> newspaperList = newspaperMapper.selectNewspapersByConditions(newspaper);
        log.info("search newspaper totalNum: {}",newspaperList.size());
        return newspaperList;
    }

    /**
     * 文件上传，Excel添加
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public Integer importNewspaper(MultipartFile file) throws Exception {
        Boolean  isUpload = UploadUtil.file(file);
        if (isUpload){
            List<Newspaper>  newspapers = ExcelUtil.importData(
                    Contant.WIN_PATH + file.getOriginalFilename(), Newspaper.class,2);
            log.info("import newspapers totalNum: {}",newspapers.size());
            for (Newspaper newspaper : newspapers){
                newspaper.setCreateTime(DateUtil.formatDate());
                newspaper.setUpdateTime(DateUtil.formatDate());
                newspaperMapper.insertNewspaper(newspaper);
            }
             UploadUtil.deleteDir();
            return newspapers.size();
        }
        return 0;
    }

    /**
     * 添加报纸
     * @param newspaper
     * @return
     */
    @Override
    public int addNewspaper(Newspaper newspaper) {
        log.info(newspaper.toString());
        newspaper.setCreateTime(DateUtil.formatDate());
        newspaper.setUpdateTime(DateUtil.formatDate());
        return newspaperMapper.insertNewspaper(newspaper);
    }

    /**
     * 根据ID查询报纸
     * @param id
     * @return
     */
    @Override
    public Newspaper queryNewspaperById(String id) {
        log.info("query id= {}",id);
        Newspaper newspaper = newspaperMapper.selectNewspaperById(Integer.parseInt(id));
        return newspaper;
    }

    /**
     * 更新报纸信息
     * @param newspaper
     */
    @Override
    public void modifyNewspaper(Newspaper newspaper){
        log.info("update newspaper",newspaper);
        newspaper.setUpdateTime(DateUtil.formatDate());
        newspaperMapper.updateNewspaper(newspaper);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void removeNewspapers(Integer[] ids) {
        log.info(ids.toString());
        newspaperMapper.deleteNewspaperByIds(ids);
    }
}
